﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cat;
using MyDBconnection;
using System.Data.Odbc;

namespace Thesis_2
{
    public partial class frmChangePassword : Form
    {
        OdbcCommand cmd = null;
        OdbcConnection con = null;
        OdbcDataReader rdr = null;
        ConnectionString cs = new ConnectionString();
        DatabaseConnection dbcon = new DatabaseConnection();
        
        public frmChangePassword()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmChangePassword_Load);
            btnreset.Click += new EventHandler(btnreset_Click);
            txtCurrentPass.TextChanged += new EventHandler(txtCurrentPass_TextChanged);
            btnprocess.Click += new EventHandler(btnprocess_Click);
        }

        void btnprocess_Click(object sender, EventArgs e)
        {
            if (txtCurrentPass.Text == "")
            {
                MessageBox.Show("Please enter current password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCurrentPass.Focus();
                return;

            }
            dbcon.dbconnection();
            dbcon.mylogin("SELECT * from tbluserinfo WHERE Username = '" + txtUser.Text + "' and Password= '" + txtCurrentPass.Text + "'");
            if (dbcon.tbl.Rows.Count > 0)
            {
                dbcon.DBClose();
             if (txtNewpass.Text == "")
             {
                MessageBox.Show("Please Enter New password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtNewpass.Focus();
                return;
            
             }
             if (txtNewpass.TextLength < 5)
             {
                MessageBox.Show("The New Password Should be of Atleast 5 Characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtNewpass.Focus();
                return;

             }
             if (txtRepass.Text == "")
             {
                MessageBox.Show("Please Confirm password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRepass.Focus();
                return;

             }
             if (txtNewpass.Text != txtRepass.Text)
             {
                 MessageBox.Show("Password do not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 txtCurrentPass.Text = "";
                 txtNewpass.Text = "";
                 txtRepass.Text = "";
                 txtCurrentPass.Focus();
                 return;
             }
             if (txtNewpass.Text == txtCurrentPass.Text)
             {
                 MessageBox.Show("Your New password is the same with your current password Please Enter new password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 txtNewpass.Text = "";
                 txtRepass.Text = "";
                 txtNewpass.Focus();
                 return;
             }
             dbcon.dbconnection();
             dbcon.UpdateData("Update tbluserinfo set Password='" + txtNewpass.Text + "' Where Username='" + txtUser.Text + "';");
             MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
             dbcon.DBClose();
             }
             else
             {
                
                label1.Text = "Current password incorrect";
                txtCurrentPass.Text = "";
                txtNewpass.Text = "";
                txtRepass.Text = "";
                txtCurrentPass.Focus();
                dbcon.DBClose();
                return;
            }

        }

        void txtCurrentPass_TextChanged(object sender, EventArgs e)
        {
            if (txtCurrentPass.Text != "")
            {
                txtNewpass.Enabled = true;
                txtRepass.Enabled = true;
                return;
            }
            else
            {
                txtNewpass.Enabled = false;
                txtRepass.Enabled = false;
            }
        }
        void btnreset_Click(object sender, EventArgs e)
        {
            txtCurrentPass.Text = "";
            txtNewpass.Text = "";
            txtRepass.Text = "";
            txtNewpass.Enabled = false;
            txtRepass.Enabled = false;
        }

        void frmChangePassword_Load(object sender, EventArgs e)
        {
            userlog();
        }
        void userlog()
        {
            con = new OdbcConnection(cs.DBConn);
            con.Open();
            string us = "Select * from tbluserlog";
            cmd = new OdbcCommand(us);
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                txtUser.Text = (rdr["Username"].ToString());


            }

        }
    }
}
